//
//  LEDBTool.m
//  CreditAddressBook
//
//  Created by Lee on 15/10/14.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "LEDBTool.h"

static FMDatabaseQueue *_queue;
static FMDatabase *_db;
static NSString *_DBPath;

@implementation LEDBTool

+ (void)initialize {
    //获取沙盒中数据库的名称
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"by_call_history.sqlite"];
    _DBPath = path;
    
    //创建队列
    _queue = [FMDatabaseQueue databaseQueueWithPath:path];
    //创建数据库
    _db = [FMDatabase databaseWithPath:path];
    
    //收藏表
    [_db executeUpdate:@"create table if not exists t_call_collection (id text primary key UNIQUE not null, phone text, name text, headimage_name text, create_time INTEGER, recordID INTEGER);"];
    
    [_queue inDatabase:^(FMDatabase *db) {
        
    }];
}

+ (NSString *)DBPath {
    return _DBPath;
}

+ (FMDatabase *)sharedDatabase {
    if (!_db) {
        _db = [FMDatabase databaseWithPath:_DBPath];
    }
    return _db;
}

+ (FMDatabaseQueue *)sharedQueue {
    if (!_queue) {
        _queue = [FMDatabaseQueue databaseQueueWithPath:_DBPath];
    }
    return _queue;
}

+ (BOOL)createTableWithSql:(NSString *)sql {
    return [_db executeUpdate:sql];
}
@end
